﻿namespace Jxym.DBUtility
{
    using System;
    using System.Configuration;

    public sealed class WebConfigHelper
    {
        public static bool GetConfigBool(string key)
        {
            bool result = false;
            string cfgVal = GetConfigString(key);
            if ((cfgVal != null) && (string.Empty != cfgVal))
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                }
            }
            return result;
        }

        public static decimal GetConfigDecimal(string key)
        {
            decimal result = 0M;
            string cfgVal = GetConfigString(key);
            if ((cfgVal != null) && (string.Empty != cfgVal))
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                }
            }
            return result;
        }

        public static int GetConfigInt(string key)
        {
            int result = 0;
            string cfgVal = GetConfigString(key);
            if ((cfgVal != null) && (string.Empty != cfgVal))
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                }
            }
            return result;
        }

        public static string GetConfigString(string key)
        {
            string CacheKey = "AppSettings-" + key;
            object objModel = DataCacheHerpary.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.AppSettings[key];
                    if (objModel != null)
                    {
                        DataCacheHerpary.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180.0), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return objModel.ToString();
        }
    }
}

